<template>
	<div class="page">
		<view class="driver_info">
			<up-cell-group class="basic_info">
				<up-cell title="姓名" :value="info.name"></up-cell>
				<up-cell title="手机号" :value="info.phoneNumber" isLink>
				</up-cell>
				<up-cell title="身份证号" :value="info.idNumber"></up-cell>
				<up-cell title="证件管理" :value="certificateStatus" isLink @click="updateInfo"></up-cell>
			</up-cell-group>
			<up-cell-group>
				<up-cell title="电子签认证" :value="info.anxinqianConfirm ? '已认证' : '未认证'" isLink
					@click="toVerifyAnxinqian"></up-cell>
				<up-cell title="活体认证" :value="realnameStatus"></up-cell>
			</up-cell-group>
		</view>
	</div>

	<up-toast ref="uToastRef"></up-toast>
</template>

<script setup>
import {
	ref,
	reactive,
	onMounted
} from "vue"
import {
	getInfo as getDriverInfo,
	openAnxinqian,
	judgeIdentify,
	getAnxinqianOpenStatus
} from '@/api/driver.js'
import {
	getInfo as getCaptainInfo
} from '@/api/captain.js'
import {
	getUserProfile
} from "@/api/user"
import {
	storeToRefs
} from "pinia"
import {
	useUserStore
} from "@/store"


/* hooks */
const {
	role
} = storeToRefs(useUserStore())


/* 数据 */
const uToastRef = ref(null)
// 信息
const info = reactive({
	name: '',
	phoneNumber: '',
	anxinqianId: undefined,
	anxinqianConfirm: undefined
})
// 实名状态
const realnameStatus = ref('')
const certificateStatus = ref('')


/* 函数 */
// 电子签
const toVerifyAnxinqian = async () => {
	uToastRef.value.show({
		message: '正在获取电子签状态',
		type: 'loading',
		duration: -1
	})
	let statusRes = await getAnxinqianOpenStatus()
	uToastRef.value.hide()
	if (statusRes.success) {
		if (statusRes.data) {
			// 已开通
			uni.navigateTo({
				url: '/package_mine/pages/auth/code?delta=2'
			})
		} else {
			// 未开通，要先开通
			let {
				name,
				phoneNumber,
				idNumber
			} = info
			// 开通电子签
			uToastRef.value.show({
				message: '正在获取电子签状态',
				type: 'loading',
				duration: -1
			})
			let res = await openAnxinqian({
				name,
				phoneNumber,
				idNumber
			})
			uToastRef.value.hide()
			if (res.success) {
				uni.navigateTo({
					url: '/package_mine/pages/auth/code?delta=2'
				})
			}
		}
	}
}
// 获取用户信息
const getUserInfo = async () => {
	// 先获取用户状态
	uToastRef.value.show({
		message: '正在验证用户实名状态',
		type: 'loading',
		duration: -1
	})
	let statusRes = await judgeIdentify()
	uToastRef.value.hide()
	if (!statusRes.success) return
	let status = statusRes.data
	if (status.includes("NOT_REAL")) {
		realnameStatus.value = '未实名'
	} else if (status.includes("DRIVER_REAL")) {
		realnameStatus.value = '司机已认证'
	} else if (status.includes("CAPTAIN_REAL")) {
		realnameStatus.value = '船长已认证'
	} else if (status.includes("ASSIGNOR_REAL")) {
		realnameStatus.value = '委托人已认证'
	} else if ((status.includes("ASSIGNOR_REAL") && status.includes("ASSIGNOR_REAL")) || (status.includes(
		"CAPTAIN_REAL") && status.includes("ASSIGNOR_REAL"))) {
		realnameStatus.value = '已认证'
	}


	uToastRef.value.show({
		message: '正在获取用户信息',
		type: 'loading',
		duration: -1
	})
	let res = null
	if (role.value === 'driver') {
		res = await getDriverInfo()
	} else {
		res = await getCaptainInfo()
	}
	uToastRef.value.hide()
	if (!res.success) return
	uToastRef.value.show({
		message: '正在获取用户信息',
		type: 'loading',
		duration: -1
	})
	let res2 = await getUserProfile()
	uToastRef.value.hide()
	if (!res2.success) return
	let {
		anxinqianId,
		anxinqianConfirm
	} = res2.data
	if (status==='ASSIGNOR_REAL') {
		Object.assign(info, {
			...res2.data,
			anxinqianId,
			anxinqianConfirm,
			name: res2.data.name ,
			phoneNumber: res2.data.phone,
			idNumber: res2.data.idCard
		})
	} else {
		Object.assign(info, {
			...res.data,
			anxinqianId,
			anxinqianConfirm,
			name: res.data.name || res.data.captainName,
			phoneNumber: res.data.phoneNumber || res.data.captainPhone,
			idNumber: res.data.idNumber || res.data.idCard
		})
	}
	if (role.value === 'driver') {
		let {
			certificateFrontPic,
			certificateBackPic,
			driverLicensePic,
			driverLicensePic2,
			qualCertificate
		} = info
		certificateStatus.value = certificateFrontPic && certificateBackPic && driverLicensePic &&
			driverLicensePic2 &&
			qualCertificate ? '已上传' :
			!certificateFrontPic && !certificateBackPic && !driverLicensePic && !driverLicensePic2 && !
				qualCertificate ? '未上传' :
				'未完全上传'
	} else {
		let {
			idCardPic1,
			idCardPic2,
			certificate,
			certificate2
		} = info
		certificateStatus.value = idCardPic1 && idCardPic2 && certificate && certificate2 ? '已上传' :
			!idCardPic1 && !idCardPic2 && !certificate && !certificate2 ? '未上传' :
				'未完全上传'
	}
}
// 跳转修改信息页面
const updateInfo = () => {
	let roles = ''
	if (role.value === "driver") {
		if (realnameStatus.value === '司机已认证') {
			roles = 'driver'
		} else if (realnameStatus.value === '委托人已认证') {
			uni.navigateTo({
				url: "/package_mine/pages/auth/driverAuth"
			})
			return
		} else if (realnameStatus.value === '已认证') {
			roles = 'driver'
		}
		uni.navigateTo({
			url: "/package_mine/pages/auth/updateDriverInfo?role=" + roles
		})
	} else {
		if (realnameStatus.value === '船长已认证') {
			roles = 'captain'
		} else if (realnameStatus.value === '委托人已认证') {
			uni.navigateTo({
				url: "/package_mine/pages/auth/captainAuth"
			})
			return
		} else if (realnameStatus.value === '已认证') {
			roles = 'captain'
		}
		uni.navigateTo({
			url: "/package_mine/pages/auth/updateCaptainInfo?role=" + roles
		})
	}
}
// 初始化封装
const init = async () => {
	await getUserInfo()
}



/* 钩子 */
onMounted(() => {
	init()
})
</script>

<style lang="scss" scoped>
.page {
	background-color: #f1f1f1;
	height: 100vh;
}

.driver_info {
	padding: 10px;

	>view {
		margin-bottom: 10px;
		border-radius: 10px;
		background-color: #fff;
	}

	.basic_info {
		.change_phone {
			font-size: 12px;
			color: rgb(0, 122, 255);
			margin-left: 5px;
		}
	}
}
</style>